import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '@/stores/useUserStore'
import { showConfirmDialog } from 'vant';
import HomeView from '../views/HomeView.vue'
import ClassificationView from '../views/ClassificationView.vue'
import MyView from '../views/MyView.vue'
import ShoppingCartView from '../views/ShoppingCartView.vue'
import LsfLogistics from '../views/LsfLogistics.vue'
import CostCompositionhlf from '../views/CostCompositionhlf.vue'
import NewPeoplehlf from '../views/NewPeoplehlf.vue'
import HlfMessage from '../views/HlfMessage.vue'
import HlfNotice from '../views/HlfNotice.vue'
import HlfOrder from '../views/HlfOrder.vue'
import HlfService from '../views/HlfService.vue'
import ListItem from '../views/ListItem.vue'
import LongCategoryView from '@/views/LongCategoryView.vue'
import LoginView from '../views/LoginView.vue'
import SearchView from '../views/SearchView.vue'
import HlfRegister from '../views/HlfRegister.vue'
import HlfQueryr from '../views/HlfQueryr.vue'
import HlfDaily from '../views/HlfDaily.vue'
import LsfFavoriteshops from "../views/LsfFavoriteshops.vue";
import MyOrderView from '../views/MyOrderView.vue'
// import ZhCoupon from '@/views/ZhCoupon.vue'
import PingLun from '../views/PingLun.vue'
import PersonalCenter from '../views/PersonalCenter.vue'



const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'home',
      component: HomeView,
      meta: {
      isshowbar: true
    }
    },
    {
      path: '/classification',
      name: 'classification',
      component: ClassificationView,
      meta: {
        isshowbar: true
      }
    },
    {
      path: '/my',
      name: 'my',
      component: MyView,
      meta: {
        isshowbar: true
      }
    },
    {
      path: '/login',
      name: 'login',
      component: LoginView,
    },
    {
      path: '/shoppingcart',
      name: 'shoppingcart',
      component: ShoppingCartView,
      meta: {
        isshowbar: true
      }
    },
    {
      path: '/lsflogistics',
      name: 'lsflogistics',
      component: LsfLogistics,
    },
    {
      path: '/newpeoplehlf',
      name: 'newpeoplehlf',
      component: NewPeoplehlf,
    },
    {
      path: '/costcompositionhlf',
      name: 'costcompositionhlf',
      component: CostCompositionhlf,
    },
    // 消息中心
    {
      path: '/hlfmessage',
      name: 'hlfmessage',
      component: HlfMessage,
    },
  // 公告
    {
      path: '/hlfnotice',
      name: 'hlfnotice',
      component: HlfNotice,
    },
    // 订单
    {
      path: '/hlforder',
      name: 'hlforder',
      component: HlfOrder,
    },
    // 服务
    {
      path: '/hlfservice',
      name: 'hlfservice',
      component: HlfService,
    },
    {
      path: '/listitem',
      name: 'listitem',
      component: ListItem,
      props: route => ({ id: route.query.id })
    },
    {
      path: '/search',
      name: 'search',
      component:SearchView,
    },
    // 注册
    {
      path: '/hlfregister',
      name: 'hlfregister',
      component:HlfRegister,
    },
    // 悠享购
    {
      path: '/hlfqueryr',
      name: 'hlfqueryr',
      component:HlfQueryr,
    },
    // 雅虎日拍
    {
      path: '/hlfdaily',
      name: 'hlfdaily',
      component:HlfDaily,
    },
    {
      path: '/category',
      name: 'category',
      component: LongCategoryView,
    },
    // 卖家收藏
    {
      path: '/LsfFavoriteshops',
      name: 'LsfFavoriteshops',
      component: LsfFavoriteshops,
      props: route => ({ id: route.query.id })
    },
    {
      path: '/myorder',
      name: 'myorder',
      component:MyOrderView,
    },
    {
      path: '/trick',
      name: 'trick',
      component: () => import("../views/ZhCoupon.vue")
      
    },
    {
      path: '/pinglun',
      name: 'pinglun',
      component:PingLun,
    },
    
    // 个人中心
    {
      path: '/personal',
      name: 'personal',
      component:PersonalCenter,
    },
    // 订单详情
    {
      path:'/placeorder',
      name:'placeorder',
      component:() => import('@/views/PlaceOrder.vue'),
    },
    // 修改地址
    {
      path:'/address',
      name:'address',
      component:() => import('@/views/AddRess.vue'),
    },
    // 我的收藏
    {
      path:'/mycollect',
      name:'mycollect',
      component:() => import('@/views/MyCollect.vue'),
    },
    // 地址列表
    {
      path:'/addresslist',
      name:'addresslist',
      component:() => import('@/views/AddressList.vue'),
    }
  ],
})

// 添加路由守卫
router.beforeEach((to, from, next) => {
  const userStore = useUserStore()
  
  // 检查是否需要登录
  if (to.name === 'shoppingcart' && !userStore.isLoggedIn) {
    // 如果用户未登录，重定向到登录页
    showConfirmDialog({
      title: '登录',
      message:
        '您还没有登录，请先登录！',
    })
      .then(() => {
        // on confirm
        next({ name: 'login' })
      })
      .catch(() => {
        // on cancel
      });
   
  } else {
    // 允许访问
    next()
  }
})

export default router
